package com.lemon.smartsafety.ui.activity.fire.item.statistics;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.http.HttpBack;
import com.lemon.smartsafety.http.HttpOkBase;
import com.lemon.smartsafety.http.HttpPath;
import com.lemon.smartsafety.mode.adapter.LampMode;
import com.lemon.smartsafety.result.BaseMode;
import com.lemon.smartsafety.result.fire.FireCensusInfo;
import com.lemon.smartsafety.result.fire.FireCompanyBaseScore;
import com.lemon.smartsafety.result.fire.SuperviseCompanyDetail;
import com.lemon.smartsafety.result.fire.SuperviseCompanyResult;
import com.lemon.smartsafetynew.ui.activity.base.NewBaseActivity;
import com.lemon.smartsafety.ui.activity.fire.FirefightingUtil;
import com.lemon.smartsafety.ui.adapter.fire.LampCenterAdapter;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.JsonUtils;
import com.lemon.smartsafety.utils.app.ToastUtil;
import com.lemon.smartsafety.view.ViewFireCompanyNone;
import com.lemon.smartsafety.view.ViewFireTop;
import com.lemon.smartsafety.view.list.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class CensusStatisticsActivity extends NewBaseActivity {

    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @Bind(R.id.fireTop)
    ViewFireTop fireTop;
    @Bind(R.id.listView)
    MyListView listView;
    @Bind(R.id.scrollView)
    ScrollView scrollView;
    @Bind(R.id.viewFireCompanyNone)
    ViewFireCompanyNone viewFireCompanyNone;


    private LampCenterAdapter lampCenterAdapter;
    private List<LampMode> mList;
    private LampMode lampMode;

    private SuperviseCompanyResult.ResultEntity resultEntity;
    private FireCompanyBaseScore fireCompanyBaseScore;

    private List<FireCensusInfo> fireCensusInfoList;
    private SuperviseCompanyDetail.MyCompanyScoreListEntity myCompanyScoreListEntity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getBundleArgs() {
        if (getIntent().getExtras() != null) {
            resultEntity = (SuperviseCompanyResult.ResultEntity) getIntent().getSerializableExtra(BundleKey.FIRE_COMPANY_BASE_INFO);
            myCompanyScoreListEntity = (SuperviseCompanyDetail.MyCompanyScoreListEntity) getIntent().getSerializableExtra(BundleKey.FIRE_COMPANY_PRO_CODE);
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_census_statistics;
    }

    @Override
    protected void initTitleView() {
        setBarTitle("户籍化上报率");
    }

    @Override
    protected void initView() {

        fireTop.setTitleContent(resultEntity.getCompanyName());
        fireTop.setTitleSubContent("");
        fireTop.setNum1Content("0");
        fireTop.setNum2Content("/0");
        fireTop.setNum1Color(R.color.colorAppRed);
        fireTop.setNum2Color(R.color.colorAppBlue);
        fireTop.setImageCircleShow(true);
        fireTop.setImageCircleBg(R.drawable.shape_circle_red);

        fireCensusInfoList = new ArrayList<>();
        mList = new ArrayList<>();
        lampCenterAdapter = new LampCenterAdapter(mActivity, mList);
        listView.setAdapter(lampCenterAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (fireCensusInfoList.get(position).getType().equals("month")) {

                    intent = new Intent(mActivity, CalendarStatisticsActivity.class);
                    bundle = new Bundle();
                    bundle.putSerializable(BundleKey.FIRE_GENSUS_TAG, fireCensusInfoList.get(position));
                    bundle.putSerializable(BundleKey.FIRE_COMPANY_BASE_INFO, resultEntity);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }

            }
        });


    }

    @Override
    protected void initHttp() {
        initTopHttp();
        httpList();
    }

    @Override
    protected void initList() {

    }

    @Override
    protected void loadList() {

    }

    @Override
    protected void refreshView() {

    }

    /**
     * 错误的情况下 刷新
     */
    public void errorView(String title) {

        try {
            if (CommonUtil.listIsEmpty(mList)) {
                viewFireCompanyNone.setVisibility(View.VISIBLE);
                listView.setVisibility(View.GONE);
                if (CommonUtil.strIsEmpty(title)) {
                    viewFireCompanyNone.setContent("没有更多数据");
                    viewFireCompanyNone.setSearch("重试");
                } else {
                    viewFireCompanyNone.setContent(title);
                    viewFireCompanyNone.setSearch("重试");
                }
                viewFireCompanyNone.setAllClick(new ViewFireCompanyNone.AllClick() {
                    @Override
                    public void click() {
                        initList();
                    }
                });
            } else {
                viewFireCompanyNone.setVisibility(View.GONE);
                listView.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void callFinish() {
        mActivity.finish();
    }


    public void initTopHttp() {
        LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");
        params.put("company_code", resultEntity.getCompanyCode());
        params.put("radio_code", myCompanyScoreListEntity.getTargetCode());

        showLoading("");
        HttpOkBase.httpBasePost(params, HttpPath.getmBase(HttpPath.Get_score), "", new HttpOkBase.HttpInterface() {
            @Override
            public void callBackFunction(boolean httpClient, String result) {

                dismissLoading();
                if (httpClient) {
                    BaseMode baseMode = JsonUtils.fromJson(result, BaseMode.class);
                    if (!CommonUtil.objIsEmpty(baseMode)) {
                        if (HttpBack.httpBackError(baseMode)) {

                            try {
                                fireCompanyBaseScore = mGson.fromJson(mGson.toJson(baseMode.getResult()), FireCompanyBaseScore.class);
                                initTopScore();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        } else {
                            errorView(baseMode.getMsg());
                            ToastUtil.show(baseMode.getMsg());
                        }
                    }
                } else {
                    fireCompanyBaseScore = null;
                    errorView("网络连接失败");
                }
            }
        });

    }

    /**
     * 初始化顶部信息
     */
    private void initTopScore() {
        fireTop.setNum1Color(FirefightingUtil.getScoreTextColor(fireCompanyBaseScore.getStatus()));
        fireTop.setNum2Color(R.color.colorAppBlue);
        fireTop.setNum1Content(fireCompanyBaseScore.getCompanyScore());
        fireTop.setNum2Content("/" + fireCompanyBaseScore.getAvgScore());
        fireTop.setImageCircleBg(FirefightingUtil.getScoreImgCircleColor(fireCompanyBaseScore.getStatus()));
    }


    /**
     * 获取列表
     */
    private void httpList() {
        if (!bPutHttp) {
            bPutHttp = true;
            LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");

            params.put("company_code", resultEntity.getCompanyCode());


            showLoading("");
            HttpOkBase.httpBasePost(params, HttpPath.getmBase(HttpPath.Get_census_info), "", new HttpOkBase.HttpInterface() {
                @Override
                public void callBackFunction(boolean httpClient, String result) {

                    dismissLoading();
                    bPutHttp = false;
                    if (httpClient) {
                        BaseMode baseMode = JsonUtils.fromJson(result, BaseMode.class);
                        if (!CommonUtil.objIsEmpty(baseMode)) {
                            if (HttpBack.httpBackError(baseMode)) {

                                try {
                                    fireCensusInfoList = mGson.fromJson(mGson.toJson(baseMode.getResult()), new TypeToken<List<FireCensusInfo>>() {
                                    }.getType());
                                    transList();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            } else {
                                strShowMsg = baseMode.getMsg();
                                handler.sendEmptyMessage(SHOW_MSG);
                            }
                            lampCenterAdapter.notifyDataSetChanged();
                        }
                    }
                }
            });
        }

    }


    /**
     * 转换list
     */
    private void transList() {

        for (int i = 0; i < fireCensusInfoList.size(); i++) {
            if (CommonUtil.strIsEmpty(fireCensusInfoList.get(i).getTypeName())) {
                switch (fireCensusInfoList.get(i).getType()) {
                    case "month":
                        lampMode = new LampMode(0, fireCensusInfoList.get(i).getMonth() + "月", fireCensusInfoList.get(i).getReportCount() + "/" + fireCensusInfoList.get(i).getDayCount(), true, FirefightingUtil.getScoreImgCircleColor(fireCensusInfoList.get(i).getStatus()));
                        mList.add(lampMode);
                        break;
                    case "year":
                        lampMode = new LampMode(0, fireCensusInfoList.get(i).getYear(), fireCensusInfoList.get(i).getReportCount() + "/" + fireCensusInfoList.get(i).getDayCount(), true, FirefightingUtil.getScoreImgCircleColor(fireCensusInfoList.get(i).getStatus()));
                        mList.add(lampMode);
                        break;
                }
            } else {
                if (fireCensusInfoList.get(i).getTypeName().contains("度")) {
                    lampMode = new LampMode(1, fireCensusInfoList.get(i).getTypeName());
                } else {
                    lampMode = new LampMode(1, fireCensusInfoList.get(i).getTypeName() + "度");
                }
                mList.add(lampMode);
            }

        }

        lampCenterAdapter.notifyDataSetChanged();

        errorView("");
    }


}
